package io.kiki.sba.registry.server.clientInterface.resource;

import io.kiki.sba.registry.common.model.GenericResponse;
import io.kiki.sba.registry.common.model.multi.cluster.DataCenter;
import io.kiki.sba.registry.server.clientInterface.multi.cluster.DataCenterCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;


@Path("openapi/v1/metadata")
public class MetadataCacheResource {

    private static final Logger logger = LoggerFactory.getLogger(MetadataCacheResource.class);

    @Autowired
    DataCenterCache dataCenterCache;

    @GET
    @Path("/cache")
    @Produces(MediaType.APPLICATION_JSON)
    public GenericResponse<Object> getMetadataCache(@QueryParam("dataCenter") String dataCenter) {
        logger.info("[getMetadataCache] begin dataCenter={}", dataCenter);
        try {
            DataCenter metadata = dataCenterCache.of(dataCenter);
            return new GenericResponse<>().fillSucceed(metadata);
        } catch (Throwable th) {
            logger.error("[getMetadataCache]", th);
            return new GenericResponse<>().fillFailed(th.getMessage());
        } finally {
            logger.info("[getMetadataCache] end dataCenter={}", dataCenter);
        }
    }
}
